package com.allianz.webapps.controllers;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//import cn.org.bjca.anysign.client.security.SecurityEngineDeal;
//import cn.org.bjca.anysign.model.AnywriteSignInfo;

import com.allianz.frame.utils.PropertiesUtil;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.github.pagehelper.StringUtil;
@Controller
@RequestMapping(value = "main")
public class PDFshowController {
	
	//http://localhost:9000/pad_marketing/main/showPDF.action
	@RequestMapping(value = "showPDF")
	@ResponseBody
	public void showPDF(HttpServletRequest request, HttpServletResponse response)throws Exception {
		//通过接口获取pdf文件，还没有接口
		

		//展示pdf文件
		viewPDF(request,response);
	}

	//签字的pdf
	@RequestMapping(value = "signPDF")
	public void signPDF(HttpServletRequest request, HttpServletResponse response)throws Exception {
		//通过接口获取pdf文件，还没有接口

		//展示pdf文件
		viewPDF(request,response);
	
	}
	
	//http://portal.allianz.com.cn/pad_marketing/main/proposal325535PDF.action
	//签字的投保单pdf//http://localhost:9000/pad_marketing/main/proposal325535PDF.action
	@RequestMapping(value = "proposal325535PDF")
	@ResponseBody
	public void proposal325535PDF(HttpServletRequest request, HttpServletResponse response)throws Exception {
		//注意，需要一个投保单号参数
		//展示pdf文件
//		HttpSession hs = request.getSession();
//		User u = (User)hs.getAttribute("username");
//		if(u==null || StringUtil.isEmpty(u.getUserId())){
//			return;
//		}

//		File f = new File(PropertiesUtil.getResourcePath()+"private/pdf/"+u.getUserId()+"/success_Proposal325535.pdf");
		File f = new File(PropertiesUtil.getResourcePath()+"private/pdf/success_Proposal325535.pdf");
		String filename = f.getName();
		int length = 0;
		ServletOutputStream op = response.getOutputStream();
		response.setContentLength((int) f.length());
		response.setHeader("Content-type", "application/pdf");
		response.setHeader("Content-Disposition", "inline; filename=\""+ filename + "\"");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);

		byte[] bytes = new byte[1024];
		DataInputStream in = new DataInputStream(new FileInputStream(f));
		while ((in != null) && ((length = in.read(bytes)) != -1)) {
			op.write(bytes, 0, length);
		}
		op.close();
		in.close();
		response.flushBuffer();
	}

	//http://portal.allianz.com.cn/pad_marketing/main/otherSignDocuments325535.action
	//签字的投保单pdf//http://localhost:9000/pad_marketing/main/otherSignDocuments325535.action
	@RequestMapping(value = "otherSignDocuments325535")
	@ResponseBody
	public void otherSignDocuments325535(HttpServletRequest request, HttpServletResponse response)throws Exception {
		//展示pdf文件
//		HttpSession hs = request.getSession();
//		User u = (User)hs.getAttribute("username");
//		if(u==null || StringUtil.isEmpty(u.getUserId())){
//			return;
//		}

//		File f = new File(PropertiesUtil.getResourcePath()+"private/pdf/"+u.getUserId()+"/success_OtherSignDocuments325535.pdf");
		File f = new File(PropertiesUtil.getResourcePath()+"private/pdf/success_OtherSignDocuments325535.pdf");
		String filename = f.getName();
		int length = 0;
		ServletOutputStream op = response.getOutputStream();
		response.setContentLength((int) f.length());
		response.setHeader("Content-type", "application/pdf");
		response.setHeader("Content-Disposition", "inline; filename=\""+ filename + "\"");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);

		byte[] bytes = new byte[1024];
		DataInputStream in = new DataInputStream(new FileInputStream(f));
		while ((in != null) && ((length = in.read(bytes)) != -1)) {
			op.write(bytes, 0, length);
		}
		op.close();
		in.close();
		response.flushBuffer();
	
	}
	
	

	public void viewPDF(HttpServletRequest request, HttpServletResponse response)throws Exception {
		//展示pdf文件
		HttpSession hs = request.getSession();
		User u = (User)hs.getAttribute("username");
		if(u==null || StringUtil.isEmpty(u.getUserId())){
			return;
		}

		File f = new File(PropertiesUtil.getResourcePath()+"private/pdf/"+u.getUserId()+"/bbb.pdf");
		String filename = f.getName();
		int length = 0;
		ServletOutputStream op = response.getOutputStream();
		response.setContentLength((int) f.length());
		response.setHeader("Content-type", "application/pdf");
		response.setHeader("Content-Disposition", "inline; filename=\""+ filename + "\"");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);

		byte[] bytes = new byte[1024];
		DataInputStream in = new DataInputStream(new FileInputStream(f));
		while ((in != null) && ((length = in.read(bytes)) != -1)) {
			op.write(bytes, 0, length);
		}
		op.close();
		in.close();
		response.flushBuffer();
	}
	
	
	
	
	public void caPDF(HttpServletRequest request, HttpServletResponse response)throws Exception {
/*		//1请求北京CA接口，获取到txt加密文件，保存到本地
		
		//2请求北京CA接口，获取到pdf文件，保存到本地
		SecurityEngineDeal sed = null;
		byte[] signPdf = null;
		byte[] encData1 = null;
		int[] rv = new int[2];
		//加密包文件
		String fileName = "F:\\工作\\完成测试\\移动端\\微信签名\\anysign_V1.3_web_2.1.25374\\anysign_rsa_xingneng.txt";
		//String fileName = "E:/resources/private/pdf/0021A0001555/aaa.txt";
		try {
			// 设置BJCAROOT根目录
			SecurityEngineDeal.setProfilePath("c:/BJCAROOT");
			// 设置应用名称
			sed = SecurityEngineDeal.getInstance();
			
			// 加密数据
			encData1 = FileUtils.readFileToByteArray(new File(fileName));
			
			// 加密数据List
			List<byte[]> dsBy = new ArrayList<byte[]>();
			dsBy.add(encData1);
			
			// 签名对象
			AnywriteSignInfo  anywriteSignInfo = new AnywriteSignInfo();
			
			// 设置业务流水号
			anywriteSignInfo.setWoNo("111");
			//设置外部传入的文件数据
			anywriteSignInfo.setPdfData(null);
			
			// 设置加密数据
			anywriteSignInfo.setDataSource(dsBy);
			// 设置PDF生成后保存服务器的路径
			anywriteSignInfo.setInputPdfPath("");
			// 设置扩展信息
			String extNote = "";
			anywriteSignInfo.setExtNote(extNote);

			// 调用签名接口
			byte[] signedPdf = sed.getPDFAnyWriteSignByObject(anywriteSignInfo, rv);

			// 判断返回值
			if (rv[0] == 0) {
				//存放PDF
				FileUtils.writeByteArrayToFile(new File("E:/resources/private/pdf/0021A0001555/aaa.pdf"), signedPdf);
			}
		} catch (Exception ee) {
			ee.printStackTrace();
		}*/	
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
		
	}
}